package mbsl.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** 
 * A very simple message logger
 * <p>
 * Instances won't use any resources until the first message is added. 
 */
public class MessageLog {
	/** 
	 * Adds an error to the log.
	 * 
	 * @param pError The error message, as a formatable String.
	 * @param pObjects The objects used to format the String.
	 */
	public void addMessage(String pError, Object... pObjects) {
		if (mErrors == null)
			mErrors = new ArrayList<String>();
		
		mErrors.add(String.format(pError, pObjects));
	}
	/**
	 * Clears the list of errors
	 */
	public void clear() {
		if (mErrors != null)
			mErrors.clear();
	}
	/** Returns true if no errors have been registered, and false otherwise */
	public boolean isEmpty() {
		return mErrors == null || mErrors.size() < 1;
	}
	/**
	 * Returns the list of errors.
	 * <p>
	 * Note that if isEmpty() returns false, this is the same list that is used internally. It is
	 * safe to manipulate. If isEmpty() returns true, the returned list might be immutable.
	 */
	public List<String> getList() {
		if (mErrors == null)
			return Collections.emptyList();
		else
			return mErrors;
	}
	
	private List<String> mErrors;
}